<?php
/**
 * Created by cuipengyu.
 * User: cuipengyu
 * Date: 2019-10-18
 */

header("Content-Type:text/html;charset=utf-8");

require_once('Lib/Common/common.php');

$s=$_REQUEST['s'];

if(strpos($s,'微信')!==false || strpos($s,'威信')!==false ){
    echo "让我轻轻的告诉你，萍萍的微信号是 juzuzuanshi ";
    return;
}

$res ='';
if(iconv_strlen($s)<=12){
	$res=movieForFeiyu($s);
}

if($res){
	echo $res;
	return;
}

$res = qingyunRobot($s);
echo $res;


//********** 函数 ************/

function movieForFeiyu($s1){

	$s1 = preg_replace('#电影#i','',$s1);
    $s1 = preg_replace('#萍萍#i','',$s1);

    $s1 = str_replace(' ','',$s1);

	if($s1==''||$s1=='最新'||$s1=='最'||$s1=='看'){
		$url = 'http://www.ffilmer.co/video/type1/-----hits.html';
	}else{
		$url = 'http://www.ffilmer.co/video/name/'.$s1.'.html';
	}

	$html = ff_file_get_contents($url);

	$hasmc = preg_match_all("#<a href=\"\/video\/detail\/(\d+?).html\" title=\"(.+?)\">#i",$html,$mc);

    // 尝试剔除第一个搜字
    if($hasmc==0 && strpos($s1,'搜')===0){
        $s1 = mb_substr($s1,1);
        $url = 'http://www.ffilmer.co/video/name/'.$s1.'.html';
        $html = ff_file_get_contents($url);
        $hasmc = preg_match_all("#<a href=\"\/video\/detail\/(\d+?).html\" title=\"(.+?)\">#i",$html,$mc);
    }

	if($hasmc){
		$res="共找到".$hasmc."个电影(只显示8条)\n";
		for($i=1;$i<=$hasmc&&$i<=8;$i++){
			$res= $res.$i." <".$mc[2][$i-1].'>: http://www.ffilmer.co/video/detail/'.$mc[1][$i-1].".html\n";
		}
		return $res;
	}



	return null;
}

function qingyunRobot($s1){

	if($s1!="萍萍"){
		$s1 = preg_replace('#萍萍#i','',$s1);
	}else{
		$s1 = preg_replace('#萍萍#i','菲菲',$s1);
	}

	$url = "http://api.qingyunke.com/api.php?key=free&appid=0&msg=".$s1;
	$res = ff_file_get_contents($url);

	$objRes = json_decode($res,true);

	if($objRes && $objRes['result'] == 0){
		$msg = $objRes['content'];  

		// 替换 菲菲 ==> 萍萍
		$msg = preg_replace('#菲菲#i',"萍萍",$msg);
		
		// 替换{br} ==> \n
		$msg = preg_replace('#\{br\}#i',"\n",$msg);

		// 替换表情 {face:ID}  ==>  [CQ:face,id=ID]
		$msg = preg_replace('#\{face:(\d+?)}#i','[CQ:face,id=$1]',$msg);
		return $msg;

	}

	return null;
}